<?php

namespace App\Http\Controllers;

use App\admin;
use App\ClubLeader;
use Auth;
use Hash;
use Illuminate\Http\Request;

class AdminController extends Controller
{
    public function login()
    {
        return view('welcome');
    }

    public function postLogin(Request $request)
    {
        $adminName = $request->admin_name;
        $password = $request->password;
        $role = $request->input('role' );
        if($role == 'administrator')
        {
            $admin = Admin::where('admin_name', $adminName);

            if (Hash::check($password, $admin->value('password'))) {
                session(['isAdminLogin' => true]);
                session()->flash('success', '登录成功！');
                return redirect()->route('userList');
            } else {
                session()->flash('danger', '很抱歉，您的用户名和密码不匹配');
                return redirect()->back();
            }
        }
        else {
            $clubLeader = ClubLeader::where('leader_name', $adminName);
            if (Hash::check($password, $clubLeader->value('password'))) {
                session(['isClubLeader' => true]);
                session()->flash('success', '登录成功！');
                session([
                    'club_id' => $clubLeader->value('club_id'),
                    'leader_id' => $clubLeader->value('id')
                ]);
                return redirect()->route('clubLeader');
            }else {
                session()->flash('danger', '很抱歉，您的用户名和密码不匹配');
                return redirect()->back();
            }
        }
    }

    public function destroy()
    {
        session()->flush();
        session()->flash('success', '您已成功退出！');
        return redirect()->route('login');
    }
}
